package com.ygu.mylist.activities;

import java.util.List;
import com.ygu.mylist.R;
import com.ygu.mylist.adapter.History_List_Adapter;
import com.ygu.mylist.adapter.Main_List_Adapter;
import com.ygu.mylist.db.DataBaseHelper;
import com.ygu.mylist.db.DBQueryImpl;
import com.ygu.mylist.db.DBQueryResult;
import com.ygu.mylist.db.OprateDataBases;

import android.support.v4.app.Fragment;
import android.app.Activity;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

public class History_Fragment extends Fragment {
	private ListView lv;
	private SQLiteOpenHelper dbh;
	private List<DBQueryResult> list_data;
	//private LvAdapter adapter;
	private OprateDataBases opdatabase;
	private History_List_Adapter lvadapter;
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView=inflater.inflate(R.layout.history_list_fragment, container, false);
		initView(rootView);
		initAdapter();
		return rootView;
	}
	
	private void initView(View rootView) {
		// TODO Auto-generated method stub
		lv = (ListView) rootView.findViewById(R.id.history_list);
	}
	private void initAdapter() {
		// TODO Auto-generated method stub
		dbh=new DataBaseHelper(getActivity());
		list_data=new DBQueryImpl(dbh.getWritableDatabase()).query_history_list();
		lvadapter=new History_List_Adapter(this, list_data);
		lv.setAdapter(lvadapter);
	}


	public void refreshAdapter(){
		new AsyncTask<Void, Void, Void>(){

			@Override
			protected Void doInBackground(Void... params) {
				// TODO Auto-generated method stub
				try {
					list_data.clear();
					List<DBQueryResult> temp=new DBQueryImpl(dbh.getWritableDatabase()).query_history_list();		
					//Log.i("执行没111", "执行没1111");
					if(temp!=null&&temp.size()>0){
						
						list_data.addAll(temp);
						
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
				return null;
			}
			@Override
			protected void onPostExecute(Void result) {
				// TODO Auto-generated method stub
				lvadapter.notifyDataSetChanged();
				
				//Log.i("执行没", "执行没");
			}		
		}.execute(null,null,null);
	}
}
